package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.ProductlistVo;
import io.jsonwebtoken.lang.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Productlist;
import com.ruoyi.system.service.IProductlistService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 底图表Controller
 *
 * @author ruoyi
 * @date 2025-04-30
 */
@RestController
@RequestMapping("/system/productlist")
public class ProductlistController extends BaseController
{
    @Autowired
    private IProductlistService productlistService;

    private static final String UPLOAD_DIR = "/project/download/img/";

    /**
     * 查询底图表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductlistVo productlist)
    {
        startPage();
        List<Productlist> list = productlistService.selectProductlistList(productlist);
        return getDataTable(list);
    }



    /**
     * 导出底图表列表
     */
//    @Log(title = "底图表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductlistVo productlist)
    {
        List<Productlist> list = productlistService.selectProductlistList(productlist);
        ExcelUtil<Productlist> util = new ExcelUtil<Productlist>(Productlist.class);
        util.exportExcel(response, list, "底图表数据");
    }

    /**
     * 获取底图表详细信息
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productlistService.selectProductlistByProductId(productId));
    }


    @GetMapping(value = "/splitImg/{productId}")
    public AjaxResult splitImg(@PathVariable("productId") Long productId)
    {
        return success(productlistService.selectProductByProductIdSplitImg(productId));
    }

    /**
     * 新增底图表
     */
//    @Log(title = "底图表", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add( @RequestParam("productImage") MultipartFile productImage,
//                           @RequestParam(value = "productId", required = false) String productId,
//                           @RequestParam String productName,
//                           @RequestParam(required = false) String productIntroduce,
//                           @RequestParam(required = false) String useStoret) throws IOException {
//        // 创建存储目录（如果不存在）
//        File uploadDir = new File(UPLOAD_DIR);
//        if (!uploadDir.exists()) {
//            uploadDir.mkdirs();
//        }
//
//        // 生成唯一的文件名
//        String fileName = UUID.randomUUID().toString() + "_" + productImage.getOriginalFilename();
//        Path filePath = Paths.get(UPLOAD_DIR, fileName);
//
//        // 将上传的文件保存到指定目录
//        Files.write(filePath, productImage.getBytes());
//        Productlist productlist = new Productlist();
//        if (null != productId && !"".equals(productId)){
//            productlist.setProductId(Long.valueOf(productId));
//        }
//        productlist.setProductName(productName);
//        productlist.setProductIntroduce(productIntroduce);
//
//        productlist.setProductImage("https://lifein.asia/download/img/" + fileName);
//
//        return toAjax(productlistService.insertProductlist(productlist));
//    }

    @Log(title = "底图表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( @RequestBody Productlist productlist) throws IOException {
        // 创建存储目录（如果不存在）
        File uploadDir = new File(UPLOAD_DIR);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }


        return toAjax(productlistService.insertProductlist(productlist));
    }

    /**
     * 修改底图表
     */
    @Log(title = "底图表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Productlist productlist)
    {
        return toAjax(productlistService.updateProductlist(productlist));
    }

    /**
     * 删除底图表
     */
    @Log(title = "底图表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productlistService.deleteProductlistByProductIds(productIds));
    }
}
